package http

import (
	"github.com/gin-gonic/gin"
	"html/template"
	"micro-oauth/config"
	"net/http"
	"net/http/pprof"
	"strings"
)

func Pong(c *gin.Context) { c.String(http.StatusOK, "pong") }

func NewRouter() *gin.Engine {
	gin.EnableJsonDecoderUseNumber()
	router := gin.Default()
	httpConfig := config.LoadHTTPConfig()
	router.SetFuncMap(template.FuncMap{
		"Contains":  strings.Contains,
		"UrlPrefix": func() string { return config.PathPrefix },
	})
	router.LoadHTMLGlob(httpConfig.TemplateDir + "/*")
	//router.Use(static.Serve("/static", static.LocalFile(httpConfig.StaticDir, true)))

	router.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong") })

	if config.Debug {
		router.GET("/debug/pprof/", gin.WrapF(pprof.Index))
		router.GET("/debug/pprof/cmdline", gin.WrapF(pprof.Cmdline))
		router.GET("/debug/pprof/profile", gin.WrapF(pprof.Profile))
		router.GET("/debug/pprof/symbol", gin.WrapF(pprof.Symbol))
		router.GET("/debug/pprof/trace", gin.WrapF(pprof.Trace))
	}

	router.GET("/oauth/getCode", GetCode)
	router.POST("/oauth/register", OauthRegister)
	router.GET("/oauth/getUser", GetUserInfo)
	//router.GET("/oauth/login", OauthLoginGet)
	router.POST("/oauth/login", OauthLoginPost)
	//router.POST("/oauth/token/introspect", IntrospectTokenPost)
	router.POST("/oauth/api/v1/pos_token", IssuePosTokenPost)
	//router.GET("/oauth/scope_list", ListHomePageScope)
	//router.POST("/oauth/api/v1/check_device", CheckDeviceExist)
	//router.POST("/oauth/api/v1/devices", AddDevice)
	//
	//router.POST("/oauth/api/v1/reset/check", CheckMail)
	//router.POST("/oauth/api/v1/reset/pre/:id", ResetUserPassWdPre)
	//router.POST("/oauth/api/v1/reset/set/:id", ResetUserPassWd)
	//router.GET("/oauth/api/v1/common/rule",GetCommonRule)
	//router.GET("/admin/api/v1/partners", ListPartner)

	oauthAPIGroup := router.Group("/oauth/api/v1")
	oauthAPIGroup.Use(Authorization())
	//{
	//	oauthAPIGroup.GET("/ping", Pong)
	//	oauthAPIGroup.GET("/me", GetMe)
	//
	//	oauthAPIGroup.GET("/users", ListUser)
	//	oauthAPIGroup.POST("/users", AddUser)
	//	oauthAPIGroup.GET("/users/:id", GetUser)
	//	oauthAPIGroup.PUT("/users/:id", UpdateUser)
	//	oauthAPIGroup.PUT("/disable/users/:id", DisableUser)
	//	oauthAPIGroup.PUT("/new/users/:id", NewUpdateUser)
	//	oauthAPIGroup.DELETE("/users/:id", DeleteUser)
	//	oauthAPIGroup.POST("/users/:id/roles", AddUserRoles)
	//	oauthAPIGroup.POST("/users/:id/rules",AddPasswordRule)
	//	oauthAPIGroup.GET("/users/:id/rule",GetUserRule)
	//	oauthAPIGroup.POST("/users/:id/rule",UpdateUserRule)
	//
	//	oauthAPIGroup.POST("/reset/force", ForceResetPassWord)
	//
	//	oauthAPIGroup.GET("/permissions", OauthListPermission)
	//
	//	oauthAPIGroup.GET("/roles", ListRoles)
	//	oauthAPIGroup.GET("/roles/:id", GetRole)
	//	oauthAPIGroup.POST("/roles", AddRole)
	//	oauthAPIGroup.PUT("/roles/:id", UpdateRole)
	//	oauthAPIGroup.DELETE("/roles/:id", DeleteRole)
	//	oauthAPIGroup.POST("/roles/:id/permissions", AddRolePermissionInfos)
	//
	//	oauthAPIGroup.GET("/clients/", ListClient)
	//
	//	oauthAPIGroup.GET("/devices", ListDevice)
	//	oauthAPIGroup.GET("/devices/:id", GetDevice)
	//	oauthAPIGroup.PUT("/devices/:id", UpdateDevice)
	//	oauthAPIGroup.DELETE("/devices/:id", DeleteDevice)
	//
	//	oauthAPIGroup.POST("/token/refresh", RefreshTokenPost)
	//	oauthAPIGroup.GET("/vendor/users", ListVendorUser)
	//	oauthAPIGroup.PUT("/vendor/users/trigger_status", TriggerVendorUserStatus)
	//	oauthAPIGroup.POST("/vendor/users", AddVendorUser)
	//	oauthAPIGroup.POST("/vendor/manager_users", AddVendorManagerUser)
	//	oauthAPIGroup.PUT("/vendor/users/pwd_reset", ResetVendorUserPwd)
	//	oauthAPIGroup.PUT("/vendor/manager_users/:id", UpdateVendorManagerUser)
	//
	//}

	adminAPIGroup := router.Group("/admin/api/v1")
	adminAPIGroup.Use(Authorization())
	//{
	//	adminAPIGroup.POST("/partners", AddPartner)
	//	adminAPIGroup.PUT("/partners/:id", UpdatePartner)
	//	adminAPIGroup.GET("/partners/:id", GetPartner)
	//	adminAPIGroup.DELETE("/partners/:id", DeletePartner)
	//
	//	adminAPIGroup.GET("/permissions", ListPermission)
	//	adminAPIGroup.POST("/permissions", AddPermission)
	//	adminAPIGroup.GET("/permissions/:id", GetPermission)
	//	adminAPIGroup.PUT("/permissions/:id", UpdatePermission)
	//	adminAPIGroup.DELETE("/permissions/:id", DeletePermission)
	//	adminAPIGroup.POST("permissions/:id/apis", AddPermissionAPIs)
	//	adminAPIGroup.GET("/permission/methods", ListPermissionMethod)
	//
	//	adminAPIGroup.GET("/apis", ListAPI)
	//	adminAPIGroup.POST("/apis", AddAPI)
	//	adminAPIGroup.PUT("/apis/:id", UpdateAPI)
	//	adminAPIGroup.GET("/apis/:id", GetAPI)
	//	adminAPIGroup.DELETE("/apis/:id", DeleteAPI)
	//
	//	adminAPIGroup.GET("/clients", ListClient)
	//	adminAPIGroup.POST("/clients", AddClient)
	//	adminAPIGroup.PUT("/clients/:id", UpdateClient)
	//	adminAPIGroup.GET("/clients/:id", GetClient)
	//	adminAPIGroup.DELETE("/clients/:id", DeleteClient)
	//
	//	adminAPIGroup.GET("/scopes", ListScope)
	//	adminAPIGroup.POST("/scopes", AddScope)
	//	adminAPIGroup.PUT("/scopes/:id", UpdateScope)
	//	adminAPIGroup.GET("/scopes/:id", GetScope)
	//	adminAPIGroup.DELETE("/scopes/:id", DeleteScope)
	//	adminAPIGroup.POST("/scopes/init",InitScopeData)
	//}

	//{
	//	router.GET("/api/v1/partners")
	//}
	return router
}